//
//  YDPointSaleConfirModelController.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2017/11/10.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "YDPointSaleConfirModelController.h"
#import "YDPointSaleConfirTableCell.h"
#import "YDPositionModel.h"

@interface YDPointSaleConfirModelController ()<UITableViewDelegate , UITableViewDataSource>
//内容容器
@property (nonatomic , weak) UIView *contentView;
//标题
@property (nonatomic , weak) UILabel *titleLabel;
//分割线
@property (nonatomic , weak) UIView *lineView;
//提示文字
@property (nonatomic , weak) UILabel *postLabel;
//取消按钮
@property (nonatomic , weak) SYNoHeightedButton *cancleButton;
//确认按钮
@property (nonatomic , weak) SYNoHeightedButton *sureButton;

//tableView
@property (nonatomic , weak) UITableView *tableView;
@end

@implementation YDPointSaleConfirModelController

-(void)viewDidLoad
{
    [super viewDidLoad];
//    DDLog(@"%@",self.model)
    
    //设置父控件的透明度会影响子空间的透明度，可以通过下面的设置来设置父控件的透明度而不影响子空间的透明度
    UIColor *color = [UIColor clearColor];
    self.view.backgroundColor = [color colorWithAlphaComponent:0.5];
    [self contentView];
    [self addChildViews];
}

-(void)addChildViews
{
    [self titleLabel];
    [self lineView];
    [self postLabel];
    [self cancleButton];
    [self sureButton];
    [self tableView];
}
/************内容view***************/
-(UIView *)contentView
{
    if (_contentView == nil) {
        UIView *contentView = [[UIView alloc] init];
        [self.view addSubview:contentView];
        contentView.backgroundColor = [UIColor whiteColor];
        [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(rate375(330)));
            make.centerX.equalTo(self.view);
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(386)));
                make.top.equalTo(self.view).offset(rateX812(126));
            }else{
                make.height.equalTo(@(rate750(386)));
                make.top.equalTo(self.view).offset(rate750(126));
            }
        }];
        contentView.layer.cornerRadius = 10.0f;
        contentView.layer.masksToBounds = YES;
        _contentView = contentView;
    }
    return _contentView;
}
/************标题***************/
-(UILabel *)titleLabel
{
    if (_titleLabel == nil) {
        UILabel *titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:titleLabel];
        titleLabel.text = @"点卖确认";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:font20ScreenWRate * LKScreenW];
        titleLabel.textColor = [UIColor colorWithHexString:dark];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contentView);
            if (isPhoneX) {
                make.top.equalTo(self.contentView.mas_top).offset(rateX812(10));
            }else{
                make.top.equalTo(self.contentView.mas_top).offset(rate750(10));
            }
        }];
        _titleLabel = titleLabel;
    }
    return _titleLabel;
}

/************分割线***************/
-(UIView *)lineView
{
    if (_lineView == nil) {
        UIView *lineView = [[UIView alloc] init];
        [self.contentView addSubview:lineView];
        lineView.backgroundColor = NumberColor(247, 247, 247);
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contentView);
            make.width.equalTo(@(rate375(284)));
            make.height.equalTo(@1);
            if (isPhoneX) {
                make.top.equalTo(self.titleLabel.mas_bottom).offset(rateX812(6));
            }else{
                make.top.equalTo(self.titleLabel.mas_bottom).offset(rate750(6));
            }
        }];
        _lineView = lineView;
    }
    return _lineView;
}
/************提示文字***************/
-(UILabel *)postLabel
{
    if (_postLabel == nil) {
        UILabel *postLabel = [[UILabel alloc] init];
        [self.contentView addSubview:postLabel];
        postLabel.numberOfLines = 0;
        postLabel.textAlignment = NSTextAlignmentLeft;
        postLabel.font = [UIFont systemFontOfSize:font11ScreenWRate * LKScreenW];
        postLabel.textColor = [UIColor colorWithHexString:paleRed];
        NSString *string = @"点卖指令可能因跌停无法交易，默认至下一个交易日挂单交易。浮动盈亏只供参考，具体以实际成交为准。";
        postLabel.attributedText = [NSString createLineWrapAttriStringWithString:string andLineSpace:2.0 withWrapSpace:0.5 Font:[UIFont systemFontOfSize:font11ScreenWRate * LKScreenW]];
        
        [postLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.lineView);
            make.centerX.equalTo(self.contentView);
            if (isPhoneX) {
                make.top.equalTo(self.lineView.mas_bottom).offset(rateX812(227));
                make.bottom.equalTo(self.contentView.mas_bottom).offset(-rateX812(69));
            }else{
                make.top.equalTo(self.lineView.mas_bottom).offset(rate750(227));
                make.bottom.equalTo(self.contentView.mas_bottom).offset(-rate750(69));
            }
        }];
        _postLabel = postLabel;
    }
    return _postLabel;
}

/************取消按钮***************/
-(SYNoHeightedButton *)cancleButton
{
    if (_cancleButton == nil) {
        SYNoHeightedButton *cancleButton = [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:cancleButton];
        cancleButton.backgroundColor = [UIColor clearColor];
        [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancleButton setTitleColor:[UIColor colorWithHexString:paleRed] forState:UIControlStateNormal];
        cancleButton.titleLabel.font = [UIFont systemFontOfSize:font16ScreenWRate * LKScreenW];
        cancleButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.lineView);
            make.width.equalTo(@(rate375(135)));
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(42)));
                make.bottom.equalTo(self.contentView.mas_bottom).offset(-rateX812(18));
            }else{
                make.height.equalTo(@(rate750(42)));
                make.bottom.equalTo(self.contentView.mas_bottom).offset(-rate750(18));
            }
        }];
        cancleButton.layer.cornerRadius = 5.0;
        cancleButton.layer.masksToBounds = YES;
        cancleButton.layer.borderColor = [UIColor colorWithHexString:paleRed].CGColor;
        cancleButton.layer.borderWidth = 1.0f;
        [cancleButton addTarget:self action:@selector(cancleButtonButtonClick:) forControlEvents:UIControlEventTouchDown];
        cancleButton.tag = 101;
        _cancleButton = cancleButton;
    }
    return _cancleButton;
}
/************确认按钮***************/
-(SYNoHeightedButton *)sureButton
{
    if (_sureButton == nil) {
        SYNoHeightedButton *sureButton = [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:sureButton];
        sureButton.backgroundColor = [UIColor colorWithHexString:paleRed];
        [sureButton setTitle:@"确定" forState:UIControlStateNormal];
        [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        sureButton.titleLabel.font = [UIFont systemFontOfSize:font16ScreenWRate * LKScreenW];
        sureButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.lineView);
            make.width.equalTo(@(rate375(135)));
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(42)));
                make.bottom.equalTo(self.contentView.mas_bottom).offset(-rateX812(18));
            }else{
                make.height.equalTo(@(rate750(42)));
                make.bottom.equalTo(self.contentView.mas_bottom).offset(-rate750(18));
            }
        }];
        sureButton.layer.cornerRadius = 5.0;
        sureButton.layer.masksToBounds = YES;
        [sureButton addTarget:self action:@selector(cancleButtonButtonClick:) forControlEvents:UIControlEventTouchDown];
        sureButton.tag = 102;
        _sureButton = sureButton;
    }
    return _sureButton;
}


#pragma 按钮点击事件
-(void)cancleButtonButtonClick:(SYNoHeightedButton *)button
{
    if (button.tag == 101) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self sellAction];
    }
}

//卖出动作
-(void)sellAction
{
    [[SYProgressHUD sharedInstance] showMBHudWithSting:@""];
    NSInteger stockID = self.model.Id;
    NSString *url = [NSString stringWithFormat:@"/buyRecord/sellapply/%zd" , stockID];
    NSString *baseUrl = [NSString stringWithFormat:@"%@%@",baseUrlHttp , url];
    NSString *authToken = [[SYUserDataManger sharedInstance] getUserToken];
    BaseNetRequest *request = [[BaseNetRequest alloc] initWithRequestMethod:POSTMethod];
    [request sessionRequestWithDictionary:nil andURLString:baseUrl andAuthToken:authToken WithSuccessBlock:^(NSDictionary *resultDict) {
        DDLog(@"%@",resultDict)
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        if ([resultDict[@"code"] integerValue] == 200) {
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"操作成功~" isExistNav:NO];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                
                [self dismissViewControllerAnimated:YES completion:^{
                    if (self.delegate &&[self.delegate respondsToSelector:@selector(saleConfirSuccess)]) {
                        [self.delegate saleConfirSuccess];
                    }
                }];
                
            });
            
        }else{
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"操作失败，请重试~" isExistNav:NO];
        }
    } failBlock:^(NSError *error) {
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        DDLog(@"%@",error)
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"操作失败，请重试~" isExistNav:NO];
    }];
}


/************tableView***************/
-(UITableView *)tableView
{
    if (_tableView == nil) {
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        [self.view addSubview:tableView];
        tableView.backgroundColor = [UIColor whiteColor];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.scrollEnabled = NO;
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contentView);
            make.left.right.equalTo(self.lineView);
            make.top.equalTo(self.lineView.mas_bottom);
            make.bottom.equalTo(self.postLabel.mas_top);
        }];
        _tableView = tableView;
    }
    return _tableView;
}

#pragma UITableViewDelegate , UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return isPhoneX ? rateX812(38) : rate750(38);
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDPointSaleConfirTableCell *cell = [YDPointSaleConfirTableCell cellWithTableView:tableView];
    if (indexPath.row == 0) {
        cell.cateLabel.text = @"股票名称:";
        cell.contLabel.text = self.model.stockName;
        cell.contLabel.textColor = [UIColor colorWithHexString:dark];
    }else if (indexPath.row == 1){
        cell.cateLabel.text = @"股票代码:";
        cell.contLabel.text = self.model.stockCode;
        cell.contLabel.textColor = [UIColor colorWithHexString:dark];
    }else if (indexPath.row == 2){
        cell.cateLabel.text = @"委托价格:";
        cell.contLabel.text = @"市价";
        cell.contLabel.textColor = [UIColor colorWithHexString:dark];
    }else if (indexPath.row == 3){
        cell.cateLabel.text = @"卖出数量:";
        cell.contLabel.text = self.model.pzNumberOfStrand;
        cell.contLabel.textColor = [UIColor colorWithHexString:dark];
    }else if (indexPath.row == 4){
        cell.cateLabel.text = @"浮动盈亏:";
        cell.contLabel.text = self.model.pzProfitOrLoss;
        if ([self.model.profitOrLoss floatValue] > 0) {
            cell.contLabel.textColor = [UIColor colorWithHexString:paleRed];
        }else{
            cell.contLabel.textColor = [UIColor colorWithHexString:leafyGreen];
        }
    }else if (indexPath.row == 5){
//        cell.cateLabel.text = @"持仓天数:";
//        cell.contLabel.text = [NSString stringWithFormat:@"%zd天",self.model.getCreatTimeFromNow];
//        cell.contLabel.textColor = [UIColor colorWithHexString:dark];
        
        cell.cateLabel.text = @"持仓类型:";
        cell.contLabel.textColor = [UIColor colorWithHexString:dark];
        if (self.model.cycle) {
            cell.contLabel.text = [NSString stringWithFormat:@"T+%ld",self.model.cycle.integerValue];
        }
        
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


@end
